package com.sk.cardeal.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.cardeal.R;
import com.sk.cardeal.callback.PriceChangedCallback;
import com.sk.cardeal.entity.NewPartsList;

import java.util.List;

/**
 * Created by sk on 2018/1/13.
 */

public class FixPartsAdapter extends BaseQuickAdapter<NewPartsList.ResultListBean.PartsQualityListBean, BaseViewHolder> {

    private PriceChangedCallback priceChangedCallback;
    public FixPartsAdapter(int layoutResId, @Nullable List<NewPartsList.ResultListBean.PartsQualityListBean> data,PriceChangedCallback priceChangedCallback) {
        super(layoutResId, data);
        this.priceChangedCallback = priceChangedCallback;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final NewPartsList.ResultListBean.PartsQualityListBean item) {

        helper.setText(R.id.project_name, item.getPartsName())
                .setText(R.id.project_code, "商品码:" + item.getPartsOeCode())
                .setText(R.id.sure_num, "×" + item.getWantCount())
                .setText(R.id.edit_num, String.valueOf(item.getWantCount()))
                .addOnClickListener(R.id.edit_btn)
                .addOnClickListener(R.id.finish_edit);

        EditText ed_price = helper.getView(R.id.edit_price);
        ed_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              if(priceChangedCallback!=null){
                  priceChangedCallback.priceChange(helper.getAdapterPosition(),Double.parseDouble(s.toString()));
              }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        final int[] count = {item.getWantCount()};

        helper.getView(R.id.reduce).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count[0] > 0) {
                    count[0]--;
                    helper.setText(R.id.edit_num, String.valueOf(count[0]));
                }
            }
        });
        helper.getView(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count[0]++;
                helper.setText(R.id.edit_num, String.valueOf(count[0]));
            }
        });

        if (item.isOpen()) {
            helper.setGone(R.id.edit_ll, true);
            helper.setGone(R.id.normal_ll, false);
        } else {
            helper.setGone(R.id.edit_ll, false);
            helper.setGone(R.id.normal_ll, true);
        }
    }
}
